package service.impl;

import global.Constants;
import global.security.SessionUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import service.AccountService;
import util.DateUtils;
import dao.AccountDAO;
import domain.Account;

/**
 * 账目Service实现类.
 * <p>
 * 创建日期：2010-07-01<br>
 * 创建人：Xiyt<br>
 * 修改日期：<br>
 * 修改人：<br>
 * 修改内容：<br>
 * 
 * @author Xiyt
 * @version 1.0
 */
public class AccountServiceImpl implements AccountService {
	/**
	 * 账目DAO
	 * */
	private AccountDAO accountDao = null;

	/**
	 * 设定账目DAO
	 * */
	public void setAccountDao(AccountDAO accountDao) {
		this.accountDao = accountDao;
	}

	/**
	 * 根据账目ID查询账目信息
	 * @param accountId 账目ID
	 * @return Account 账目
	 */
	public Account selectByPrimaryKey(int accountId) {
		Account account = accountDao.selectByPrimaryKey(accountId);
		if(null!=account){
			// 设定账目日期
			account.setAccountDateStr(DateUtils.formatDate2Str(account.getAccountDate(), DateUtils.DATE_PATTON_1));
		}
		return account;
	}

	/**
	 * 根据条件查询账目数量
	 * @param paramAccount 账目条件
	 * @return int 账目数量
	 */
	public int countByCriteria(Account paramAccount) {
		// 根据条件查询账目数量
		paramAccount = getConditions(paramAccount);
		int count = accountDao.countByCriteria(paramAccount);
		return count;
	}
	
	/**
	 * 根据条件查询账目列表
	 * @param paramAccount 账目条件
	 * @return List 账目列表
	 */
	public List<Account> selectByCriteria(Account paramAccount) {
		// 取得accountList
		paramAccount = getConditions(paramAccount);
		List<Account> accountList = accountDao.selectByCriteria(paramAccount);
		List<Account> results = new ArrayList<Account>();
		if(null != accountList){
			for(int i=0;i<accountList.size();i++){
				Account account = accountList.get(i);
				account.setAccountDateStr(DateUtils.formatDate2Str(account.getAccountDate(), DateUtils.DATE_PATTON_1));
				account.setAccountType(Constants.ACCOUNT_TYPE_MAP.get(account.getAccountType()));
				account.setUpdateDateStr(DateUtils.formatDate2Str(account.getUpdateTime(), DateUtils.DATE_TIME_PATTON_1));
				results.add(account);
			}
		}
		return results;
	}
	

	/**
	 * 根据条件查询账目列表带分页
	 * @param paramAccount 账目条件
	 * @return List 账目列表
	 */
	public List<Account> selectByCriteriaForPaging(Account paramAccount) {
		// 取得accountList
		paramAccount = getConditions(paramAccount);
		List<Account> accountList = accountDao.selectByCriteriaForPaging(
				paramAccount, paramAccount.getStart(), paramAccount.getLimit());
		List<Account> results = new ArrayList<Account>();
		if(null != accountList){
			for(int i=0;i<accountList.size();i++){
				Account account = accountList.get(i);
				account.setAccountDateStr(DateUtils.formatDate2Str(account.getAccountDate(), DateUtils.DATE_PATTON_1));
				account.setAccountType(Constants.ACCOUNT_TYPE_MAP.get(account.getAccountType()));
				account.setUpdateDateStr(DateUtils.formatDate2Str(account.getUpdateTime(), DateUtils.DATE_TIME_PATTON_1));
				results.add(account);
			}
		}
		return results;
	}

	/**
	 * 添加账目
	 * @param account 账目
	 */
	public void insert(Account account) {
		// 删除区分：未删除
		account.setDelflag(Constants.DEL_FLAG_1);
		// 系统时间
		Timestamp sysdate = new Timestamp(System.currentTimeMillis());
		account.setCreateUser(SessionUtils.getCurrentUserId());
		account.setCreateTime(sysdate);
		account.setUpdateUser(SessionUtils.getCurrentUserId());
		account.setUpdateTime(sysdate);
		accountDao.insert(account);
	}

	/**
	 * 修改账目
	 * @Description: 修改账目
	 * @param account 账目
	 */
	public void update(Account account) {
		// 系统时间
		Timestamp sysdate = new Timestamp(System.currentTimeMillis());
		account.setUpdateUser(SessionUtils.getCurrentUserId());
		account.setUpdateTime(sysdate);
		accountDao.updateByPrimaryKey(account);
	}
	

	/**
	 * 删除账目
	 * @param accountIds 账目IDS
	 */
	public void delete(String accountIds) {
		String[] accountIdArr = accountIds.split(",");
		for (int i = 0; i < accountIdArr.length; i++) {
			// 删除账目
			accountDao.deleteByPrimaryKey(Integer.parseInt(accountIdArr[i]));
		}
	}
	
	/**
	 * 计算合计
	 * @return 合计值
	 */
	public double getTotalMoney(Account paramAccount) {
		// 返回值
		double result = 0;
		// 查询
		paramAccount = getConditions(paramAccount);
		Double money = accountDao.selectSumByCriteria(paramAccount);
		if(null!=money){
			result = money.doubleValue();
		}
		
		return result;
	}
	
	/**
	 * 当前合计
	 * @param paramAccount 账目条件
	 * @return 当前合计值
	 */
	public double getCurrentMoney(Account paramAccount) {
		// 返回值
		double result = 0;
		// 查询
		paramAccount = getConditions(paramAccount);
		Double money = accountDao.selectSumByCriteria(paramAccount);
		if(null!=money){
			result = money.doubleValue();
		}
		
		return result;
	}
	
	/**
	 * 根据条件分组查询账目收支金额
	 * @param paramAccount 账目条件
	 * @return List 账目列表
	 */
	public List<Account> getGroupSumMoney(Account paramAccount){
		// 取得accountList
		List<Account> accountList = accountDao.selectGroupSumByCriteria(paramAccount);
		List<Account> results = new ArrayList<Account>();
		if(null != accountList){
			return accountList;
		}else{
			return results;
		}
	}
	
	/**
	 * 设置公共参数
	 * @param paramAccount 参数对象
	 */
	private Account getConditions(Account paramAccount){
		paramAccount.setCreateUser(SessionUtils.getCurrentUserId());// 当前用户
		return paramAccount;
	}
}
